/*这是能通过域名的字符串，获取它的IP地址等各项信息的程序！*/

#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <unistd.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <iostream>
#include <string.h>
using namespace std;

int main(int argc, char **argv)
{
    if (argc != 2)
    {
        cerr << "你应该输入程序名 + 域名！" << endl;
        exit(-1);
    }
    hostent *host = gethostbyname(argv[1]);
    if (host == nullptr)
    {
        cerr << "gethostbyname()函数调用出错！" << endl;
        exit(-1);
    }
    printf("该域名的类型是：%s\n", (host->h_addrtype == AF_INET ? "AF_INET" : "AF_INET6"));
    for (int i = 0; host->h_addr_list[i]; i++)
    {
        char ip_buffer[32];
        memset(ip_buffer, 0, sizeof(ip_buffer));
        inet_ntop(AF_INET, host->h_addr_list[i], ip_buffer, sizeof(ip_buffer));
        printf("这个域名的IP地址%d：%s\n", i + 1, ip_buffer);
    }
    for (int i = 0; host->h_aliases[i]; i++)
    {
        printf("这个域名的还有这些域名：%s\n", host->h_aliases[i]);
    }
    printf("这个域名的名字是：%s\n", host->h_name);
    return 0;
}